package com.lin.tally;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lin.tally.adapter.ChartVPAdapter;
import com.lin.tally.db.DBManager;
import com.lin.tally.frag_chart.IncomeChartFragment;
import com.lin.tally.frag_chart.OutcomeChartFragment;
import com.lin.tally.utils.CalendarDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MonthCharActivity extends AppCompatActivity {

    Button inBtn,outBtn;
    TextView dateTv,inTv,outTv;
    ViewPager chartVp;
    private int year;
    private int month;
    int selectPos=-1,selectMonth=-1;
    List<Fragment> chartFragList;
    private IncomeChartFragment incomeChartFragment;
    private OutcomeChartFragment outcomeChartFragment;
    private ChartVPAdapter chartVPAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_char);
        initView();
        initTime();
        initStatistics(year,month);
        initFrag();
        setVPSelectListener();
    }

    private void setVPSelectListener() {
        chartVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                setButtonStyle(position);
            }
        });
    }

    private void initFrag() {
        chartFragList=new ArrayList<>();
        //添加Fragment对象
        incomeChartFragment = new IncomeChartFragment();
        outcomeChartFragment = new OutcomeChartFragment();
        //添加数据到Fragment当中
        Bundle bundle = new Bundle();
        bundle.putInt("year",year);
        bundle.putInt("month",month);
        incomeChartFragment.setArguments(bundle);
        outcomeChartFragment.setArguments(bundle);
        //将Fragment添加到数据源中
        chartFragList.add(outcomeChartFragment);
        chartFragList.add(incomeChartFragment);
        //使用适配器
        chartVPAdapter = new ChartVPAdapter(getSupportFragmentManager(), chartFragList);
        //将fragment加载到Activity当中
        chartVp.setAdapter(chartVPAdapter);
    }

    /**
     * 初始化某年某月的收支情况
     */
    private void initStatistics(int year,int month) {
        float inMoneyOneMonth=DBManager.getSumMoneyOneMonth(year,month,1);//收入总钱数
        float outMoneyOneMonth=DBManager.getSumMoneyOneMonth(year,month,0);//支出总钱数
        int incountItemOneMonth=DBManager.getCountItemOneMonth(year,month,1);//收入多少笔
        int outcountItemOneMonth=DBManager.getCountItemOneMonth(year,month,0);//支出多少笔
        dateTv.setText(year+"年"+month+"月账单");
        inTv.setText("共"+incountItemOneMonth+"笔收入，￥"+inMoneyOneMonth);
        outTv.setText("共"+outcountItemOneMonth+"笔支出，￥"+outMoneyOneMonth);
    }

    /**
     * 初始化时间
     */
    private void initTime() {
       Calendar calenda= Calendar.getInstance();
       year = calenda.get(Calendar.YEAR);
       month =calenda.get(Calendar.MONTH)+1;
    }

    //初始化控件
    private void initView() {
        inBtn=findViewById(R.id.chart_btn_in);
        outBtn=findViewById(R.id.chart_btn_out);
        dateTv=findViewById(R.id.chart_tv_date);
        inTv=findViewById(R.id.chart_tv_in);
        outTv=findViewById(R.id.chart_tv_out);
        chartVp=findViewById(R.id.chart_vp);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chart_iv_back:
                finish();
                break;
            case R.id.chart_iv_rili:
                showCalendarDialog();
                break;
            case R.id.chart_btn_in:
                setButtonStyle(1);
                chartVp.setCurrentItem(1);
                break;
            case R.id.chart_btn_out:
                setButtonStyle(0);
                chartVp.setCurrentItem(0);
                break;

        }
    }

    /**
     * 显示日历的对话框
     */
    private void showCalendarDialog() {
        CalendarDialog dialog=new CalendarDialog(this,selectPos,selectMonth);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
            @Override
            public void onRefresh(int selPos, int year, int month) {
                MonthCharActivity.this.selectPos=selPos;
                MonthCharActivity.this.selectMonth=month;
                initStatistics(year,month);
                incomeChartFragment.setDate(year,month);
                outcomeChartFragment.setDate(year, month);
            }
        });
    }

    /**
     * 设置按钮样式的改变 支出-0，收入-1
     */
    private void setButtonStyle(int kind){
        if (kind==0) {
            outBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
            outBtn.setTextColor(Color.WHITE);
            inBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            inBtn.setTextColor(Color.BLACK);
        }else if(kind==1){
            inBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
            inBtn.setTextColor(Color.WHITE);
            outBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            outBtn.setTextColor(Color.BLACK);
        }
    }
}